#coding:utf-8
from django.http import Http404
from django.http import HttpResponseRedirect
from datiandou import settings
from django.core import urlresolvers
from django.contrib.auth.models import *
import logging
log = logging.getLogger(__name__)
class  ZZAuthMiddleware(object):

    def process_request(self, request):
        print('-------Check Permissions-----------')
        log.debug(request.path)
        urlconf = settings.ROOT_URLCONF
        urlresolvers.set_urlconf(urlconf)
        resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
        if hasattr(request, 'urlconf'):
            # Reset url resolver with a custom urlconf.
            urlconf = request.urlconf
            urlresolvers.set_urlconf(urlconf)
            resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
        resolver_match = resolver.resolve(request.path_info)
        ormethod=str(resolver_match.func).split(' ')[1]
        print(request.path)
        try:
            pm = Permission.objects.get(codename=ormethod)

        except Permission.DoesNotExist as e:
            log.error("没这个方法")

       # print(pm)
        log.debug('process request------------------')

    def process_response(self, request, response):
        "Send broken link emails and calculate the Etag, if needed."
        log.info("procss_response in TD")

        return response